      <h1 class="entry-title entry-title-single" itemprop="headline">SAP 基本单位与物料计量单位间的转换</h1>

    <div class="entry-content entry-content-single entry-content-aside" itemprop="text">
      <p>基本单位的转换<span class='wp_keywordlink'><a href="http://www.sapjx.com/tag/function" title="函数" target="_blank">函数</a></span>：</p>
<p><b>UNIT_CONVERSION_SIMPLE</b>  - 基本单位间的转换（如：1MIN = 60S）</p>
<p>物料单位转换的两个<span class='wp_keywordlink'><a href="http://www.sapjx.com/tag/function" title="函数" target="_blank">函数</a></span>：</p>
<p>1) <b>MD_CONVERT_MATERIAL_UNIT</b>  -  计量单位之间转换（如：每PCS 等于多少 SET）</p>
<p>2) <b>MATERIAL_UNIT_CONVERSION</b>  - 每基本单位等于多少计量单位（如：KG）,如果KZMEINH = &#8216;X&#8217; 则为每计量单位等于多少基本单位</p>
<p>&nbsp;<br />
<span id="more-578"></span><br />
1). <b>MD_CONVERT_MATERIAL_UNIT</b> 函数示例：</p>
<pre class="brush: abap;">
*&#038;---------------------------------------------------------------------*
*&#038;      Form  FRM_UT_UNIT_CONVERSION
*&#038;---------------------------------------------------------------------*
*       物料转换基本单位
*----------------------------------------------------------------------*
FORM frm_ut_unit_conversion  USING  value(pv_matnr)     value(pv_meins) 
                                    value(pv_in_meins)  value(pv_input)
                          CHANGING  ch_output TYPE ekpo-menge.

  DATA: lv_matnr      TYPE mara-matnr,
        lv_in_meins   TYPE mara-meins,
        lv_out_meins  TYPE mara-meins,
        lv_in_value   TYPE menge_d,
        lv_out_value  TYPE menge_d.

  lv_matnr     = pv_matnr.
  lv_in_meins  = pv_meins.
  lv_out_meins = pv_in_meins.
  lv_in_value  = pv_input.

  CLEAR lv_out_value.

*** 直接转换为物料计量单位，如：每PCS等于多少SET
  CALL FUNCTION 'MD_CONVERT_MATERIAL_UNIT'
    EXPORTING
      i_matnr              = lv_matnr
      i_in_me              = lv_in_meins
      i_out_me             = lv_out_meins
      i_menge              = lv_in_value
    IMPORTING
      e_menge              = lv_out_value
    EXCEPTIONS
      error_in_application = 1
      error                = 2
      OTHERS               = 3.

ENDFORM.                    " FRM_UT_UNIT_CONVERSION

</pre>
<p>&nbsp;</p>
<p>2). 自定义函数（<b style="color:red;">ZFM_UT_UNIT_CONVERSION</b>）封装上面几个函数：</p>
<pre class="brush: abap;">

FUNCTION zfm_ut_unit_conversion.
*"----------------------------------------------------------------------
*"*"Local interface:
*"  IMPORTING
*"     VALUE(IP_MATNR) LIKE  MARA-MATNR DEFAULT SPACE
*"     VALUE(IP_MEINS) LIKE  MARA-MEINS DEFAULT SPACE
*"     VALUE(IP_UNIT_IN) LIKE  T006-MSEHI
*"     VALUE(IP_UNIT_OUT) LIKE  T006-MSEHI
*"     VALUE(IP_VALUE_IN) TYPE  MENGE_D
*"  EXPORTING
*"     VALUE(EP_VALUE_OUT) TYPE  MENGE_D
*"  EXCEPTIONS
*"      OVERFLOW
*"----------------------------------------------------------------------

  DATA lv_value_meins TYPE f.

  IF ip_matnr IS INITIAL.

* 计量单位转换
    CALL FUNCTION 'UNIT_CONVERSION_SIMPLE'
      EXPORTING
        input                = ip_value_in
        round_sign           = 'X'
        unit_in              = ip_unit_in
        unit_out             = ip_unit_out
      IMPORTING
        output               = ep_value_out
      EXCEPTIONS
        conversion_not_found = 1
        division_by_zero     = 2
        input_invalid        = 3
        output_invalid       = 4
        overflow             = 5
        type_invalid         = 6
        units_missing        = 7
        unit_in_not_found    = 8
        unit_out_not_found   = 9
        OTHERS               = 10.

    IF sy-subrc = 5.
      RAISE overflow.
    ENDIF.

  ELSE.

*--------------------------------------------------------------------*
* 这一部分也可以通过 MD_CONVERT_MATERIAL_UNIT 函数换算
*--------------------------------------------------------------------*

* 物料计量单位转换：取出每计量单位等于多少基本单位
    CLEAR lv_value_meins.
    CALL FUNCTION 'MATERIAL_UNIT_CONVERSION'
      EXPORTING
        input                = ip_value_in
        kzmeinh              = 'X'
        matnr                = ip_matnr
        meinh                = ip_unit_in
        meins                = ip_meins
      IMPORTING
        output               = lv_value_meins
      EXCEPTIONS
        conversion_not_found = 1
        input_invalid        = 2
        material_not_found   = 3
        meinh_not_found      = 4
        meins_missing        = 5
        no_meinh             = 6
        output_invalid       = 7
        overflow             = 8
        OTHERS               = 9.

    IF sy-subrc = 8.
      RAISE overflow.
    ENDIF.

* 物料计量单位转换：通过基本单位换算计量单位
    CALL FUNCTION 'MATERIAL_UNIT_CONVERSION'
      EXPORTING
        input                = lv_value_meins
        kzmeinh              = space
        matnr                = ip_matnr
        meinh                = ip_unit_out
        meins                = ip_meins
      IMPORTING
        output               = ep_value_out
      EXCEPTIONS
        conversion_not_found = 1
        input_invalid        = 2
        material_not_found   = 3
        meinh_not_found      = 4
        meins_missing        = 5
        no_meinh             = 6
        output_invalid       = 7
        overflow             = 8
        OTHERS               = 9.

    IF sy-subrc = 8.
      RAISE overflow.
    ENDIF.

  ENDIF.

ENDFUNCTION.

</pre>
<p>&nbsp;</p>
<p>3). 定义Subroutine调用函数：</p>
<pre class="brush: abap;">

*&#038;---------------------------------------------------------------------*
*&#038;      Form  FRM_UNIT_CONVERSION
*&#038;---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->VALUE(PV_MATNR)  物料
*      -->VALUE(CH_MENGE)  待转换数量
*      -->VALUE(CH_MEINS)  待转换计量单位
*----------------------------------------------------------------------*
FORM frm_unit_conversion USING value(pv_matnr)
                      CHANGING value(ch_menge) value(ch_meins) .

  DATA :
         lv_in_meins    TYPE mara-meins,
         lv_out_meins   TYPE mara-meins,
         lv_in_menge    TYPE menge_d,
         lv_out_menge   TYPE menge_d.

  CLEAR : lv_in_meins,lv_out_meins, lv_in_menge, lv_out_menge.

* 取出物料基本计量单位
  SELECT SINGLE meins INTO lv_out_menge
    FROM mara
    WHERE matnr = pv_matnr.

  lv_in_menge = ch_menge.
  lv_in_meins = ch_meins.

  CALL FUNCTION 'ZFM_UT_UNIT_CONVERSION'
    EXPORTING
      ip_matnr     = pv_matnr         " 物料
      ip_meins     = lv_out_menge     " 物料计量单位
      ip_unit_in   = lv_in_meins      " 待转换 物料计量单位
      ip_unit_out  = lv_out_menge     " 转换为 物料计量单位
      ip_value_in  = lv_in_menge
    IMPORTING
      ep_value_out = lv_out_menge
    EXCEPTIONS
      overflow     = 1
      OTHERS       = 2.

  IF sy-subrc = 0.

    ch_menge = lv_out_menge.

  ENDIF.

  ch_meins = lv_out_meins.

ENDFORM.                    "FRM_UNIT_CONVERSION

</pre>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>参考文章：</p>
<p><a href="http://hi.baidu.com/gary_c/item/53d264df7eb572826dce3f57" target="_blank">http://hi.baidu.com/gary_c/item/53d264df7eb572826dce3f57</a></p>
<p><a href="http://www.cnblogs.com/wolly/archive/2010/03/25/1696087.html" target="_blank">http://www.cnblogs.com/wolly/archive/2010/03/25/1696087.html</a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>